package com.cloud.wms.tools;

import com.cloud.core.http.error.ApiException;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.ui.main.entity.RemoteEntity;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.main.entity.UserEntity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

public class TaskHelper {

    /**
     * 领用任务
     *
     * @param billId
     * @return
     */
    public static Observable<TaskEntity> receiveTask(String billId, String taskCode) {
        return FineExApplication
                .component()
                .http()
                .orderTaskList(billId)
                .map(new ResponseWrapper<>())
                .map(RemoteEntity::getItems)
                .flatMap((Function<ArrayList<TaskEntity>, ObservableSource<TaskEntity>>) Observable::fromIterable)
                .filter(taskEntity -> taskEntity.getTaskCode().equalsIgnoreCase(taskCode))
                .toList()
                .toObservable()
                .flatMap((Function<List<TaskEntity>, ObservableSource<TaskEntity>>) taskEntities -> {
                    if (taskEntities == null || taskEntities.isEmpty()) {
                        return Observable.error(new ApiException("暂无可领用任务"));
                    }
                    TaskEntity taskEntity = taskEntities.get(0);
                    Params params = new Params();
                    params.put("TaskID", taskEntity.getTaskID());
                    UserEntity userInfo = FineExApplication.component().loginCache().getUserInfo();
                    params.put("JobStationID", userInfo.getJobStationID());
                    params.put("ProductCategory", userInfo.getProductCategory());
                    params.put("ExecutorID", userInfo.getUserID());
                    return TaskHelper.receiveTask(params, taskEntity);
                })
                .flatMap((Function<TaskEntity, ObservableSource<TaskEntity>>) taskEntity -> {
                    Params params = new Params();
                    params.put("workId", taskEntity.getWorkID());
                    UserEntity userInfo = FineExApplication.component().loginCache().getUserInfo();
                    params.put("executerId", userInfo.getUserID());
                    return TaskHelper.executeTask(params)
                            .map(aVoid -> taskEntity);
                });
    }

    /**
     * 领用任务
     *
     * @param params
     * @return
     */
    public static Observable<Long> receiveTask(Params params) {
        return FineExApplication
                .component()
                .http()
                .receiveTask(params)
                .map(new ResponseWrapper<>());
    }

    /**
     * 领用任务
     *
     * @param params
     * @return
     */
    public static Observable<TaskEntity> receiveTask(Params params, TaskEntity taskEntity) {
        return FineExApplication
                .component()
                .http()
                .receiveTask(params)
                .map(new ResponseWrapper<>())
                .map(aVoid -> {
                    taskEntity.setWorkID(aVoid);
                    taskEntity.setMainBillCode(taskEntity.getBillCode());
                    taskEntity.setMainBillID(taskEntity.getBillID());
                    return taskEntity;
                });
    }

    /**
     * 领用订单任务
     *
     * @param params
     * @return
     */
    public static Observable<Long> receiveOrderTask(Params params) {
        return FineExApplication
                .component()
                .http()
                .receiveOrderTask(params)
                .map(new ResponseWrapper<>());
    }

    /**
     * 拒绝任务
     *
     * @param workId
     * @return
     */
    public static Observable<Object> refuseTask(String workId) {
        return FineExApplication
                .component()
                .http()
                .refuseTask(workId)
                .map(new ResponseWrapper<>());
    }

    /**
     * 执行任务
     *
     * @param params
     * @return
     */
    public static Observable<Object> executeTask(Params params) {
        return FineExApplication
                .component()
                .http()
                .executeTask(params)
                .map(new ResponseWrapper<>());
    }

    /**
     * 获取单据可领用任务列表
     *
     * @param billId
     * @return
     */
    public static Observable<TaskEntity> orderTaskList(long billId, String taskCode) {
        return FineExApplication
                .component()
                .http()
                .orderTaskList(billId + "")
                .map(new ResponseWrapper<>())
                .map(RemoteEntity::getItems)
                .flatMap((Function<ArrayList<TaskEntity>, ObservableSource<TaskEntity>>) Observable::fromIterable)
                .filter(taskEntity -> taskEntity.getTaskCode().equalsIgnoreCase(taskCode))
                .toList()
                .toObservable()
                .flatMap((Function<List<TaskEntity>, ObservableSource<TaskEntity>>) taskEntities -> {
                    if (taskEntities == null || taskEntities.isEmpty()) {
                        return Observable.error(new ApiException("暂无可领用任务"));
                    }
                    TaskEntity taskEntity = taskEntities.get(0);
                    return Observable.just(taskEntity);
                });
    }

    /**
     * 获取单据可领用任务列表
     *
     * @param billId
     * @return
     */
    public static Observable<TaskEntity> orderTaskList(long billId, String taskCode, String message) {
        return FineExApplication
                .component()
                .http()
                .orderTaskList(billId + "")
                .map(new ResponseWrapper<>())
                .map(RemoteEntity::getItems)
                .flatMap((Function<ArrayList<TaskEntity>, ObservableSource<TaskEntity>>) Observable::fromIterable)
                .filter(taskEntity -> taskEntity.getTaskCode().equalsIgnoreCase(taskCode))
                .toList()
                .toObservable()
                .flatMap((Function<List<TaskEntity>, ObservableSource<TaskEntity>>) taskEntities -> {
                    if (taskEntities == null || taskEntities.isEmpty()) {
                        return Observable.error(new ApiException(message));
                    }
                    TaskEntity taskEntity = taskEntities.get(0);
                    return Observable.just(taskEntity);
                });
    }
}
